package com.wshl.aijiuunion.controller.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.stx.xhb.xbanner.XBanner;
import com.wshl.aijiuunion.BaseActivity;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.NetUrl;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.utils.GlideUtils;
import com.wshl.aijiuunion.utils.ImagePreviewUtils;
import com.wshl.aijiuunion.view.bean.Bannerbean;
import com.wshl.aijiuunion.view.bean.HomeDataBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created buy 风凊扬 on 2019/11/11 0011
 * 门店详情
 */
public class StoreDetailActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.img_store_detail)
    ImageView imgStoreDetail;
    @BindView(R.id.tv_store_name)
    TextView tvStoreName;
    @BindView(R.id.tv_store_address)
    TextView tvStoreAddress;
    @BindView(R.id.tv_store_mobile)
    TextView tvStoreMobile;
    @BindView(R.id.tv_open_time)
    TextView tvOpenTime;
    @BindView(R.id.store_xbanner)
    XBanner xbanner;
    private String lat;
    private String lng;
    private String address;
    private String name;
    private String mobile;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_storedetail;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        tvTopTitle.setText("门店详情");
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }

        name = intent.getStringExtra("name");
        address = intent.getStringExtra("address");
        mobile = intent.getStringExtra("phone");
        String thumb = intent.getStringExtra("thumb");
        //   String[] images = intent.getStringArrayExtra("images");
        String images = intent.getStringExtra("images");
        lat = intent.getStringExtra("lat");
        lng = intent.getStringExtra("lng");
        String opentime = intent.getStringExtra("opentime");

        GlideUtils.loadImage(this, NetUrl.IMG + thumb, imgStoreDetail);
        tvStoreName.setText(name);
        tvStoreMobile.setText(mobile);
        tvStoreAddress.setText(address);
        tvOpenTime.setText("营业时间 " + opentime);
        if (!TextUtils.isEmpty(images)) {
            setBanner(images);
        }

    }

    private void setBanner(String images) {
        String[] split = images.split(";");

        LogUtils.i("图片个数:" + split.length);
        final List<String> bannerimgs = new ArrayList<>();
        final List<Bannerbean> banners = new ArrayList<>();
        for (String image : split) {
            banners.add(new Bannerbean(NetUrl.IMG + image));
            bannerimgs.add(NetUrl.IMG + image);
            //banners.add(new Bannerbean(image));
        }


        xbanner.setBannerData(banners);
        xbanner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                LogUtils.i("图片:" + ((Bannerbean) model).getXBannerUrl());
                GlideUtils.loadImage(StoreDetailActivity.this, ((Bannerbean) model).getXBannerUrl(), (ImageView) view);

            }
        });
        xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {

                //点击图片预览
                ImagePreviewUtils.previewImg(StoreDetailActivity.this,bannerimgs, position);

            }
        });
    }

    @OnClick({R.id.tv_store_mobile, R.id.img_back, R.id.tv_store_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.tv_store_address:
                Intent intent = new Intent(StoreDetailActivity.this, LocationActivity.class);
                intent.putExtra("lat", lat);
                intent.putExtra("lng", lng);
                intent.putExtra("address", address);
                intent.putExtra("name", name);

                ActivityUtils.startActivity(intent);
                break;
            case R.id.tv_store_mobile:

                if (!TextUtils.isEmpty(mobile)) {
                    Intent intent2 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile));
                    intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent2);
                }

                break;
        }
    }
}
